import {Component,OnInit,ViewChild} from '@angular/core';
import { flyIn } from '../../../../animations/fly-in';
import { MyHttp } from '../../../../service/MyHttp';
import { ActivatedRoute } from '@angular/router';
import { ModalDirective } from 'ng2-bootstrap/modal';
import { GlobalVariable } from '../../../../global.config';
import { Http, Response, Headers, RequestOptions,URLSearchParams } from '@angular/http';



//import {MyHttp} from '../../../../MyHttp';
declare var $:any;

@Component({
  selector: 'devinfo',
  templateUrl: './devinfo.component.html',
  styleUrls: ['./devinfo.component.css'],
  animations: [
    flyIn
  ]
})
export class Devinfo implements OnInit{
  @ViewChild('myModal') public myModal:ModalDirective;
  editdata:any;
  modify:boolean = false;
  modifyData = {qds:'',wbry:''};  
  deviceId:any;
  devdata:any;
  locker_channel_trader:any;
  locker_maintenance:any;
  locker_address:any;
  locker_category:any;
  door_name:any;
  door_size:any=-1;
  door_charge:any=-1;
  doordata:any;
  btype:number;
  door_id:any;
  deldoorname:any;
  deldoorid:any;
  door_number:any;
  constructor(private activeRoute: ActivatedRoute,private myHttp:MyHttp,private http:Http) {}
  ngOnInit() {
    this.activeRoute.params.subscribe(
      params=>{
        this.deviceId = params['deviceId']; 
        this.getdata(); 
      }

    );  
  }

  getdata(){    
    this.myHttp.post('locker/findById',{"locker_device_id" : this.deviceId}).subscribe(res=>{
      if(res.errorCode==0){
         this.devdata = res.message;
         this.getalldoor();
         //console.log(this.devdata)
      }    
      
    })
  }

  edit(data){  
    this.modify=true;
    this.locker_channel_trader = data.locker_channel_trader;
    this.locker_maintenance = data.locker_maintenance;
    this.locker_address = data.locker_address;
    this.locker_category = data.locker_category;
  }

  editsave(){
    this.myHttp.post('locker/update',{
      "locker_device_id" : this.deviceId,
      "locker_channel_trader":this.locker_channel_trader,
      "locker_maintenance" :this.locker_maintenance,
      "locker_address":this.locker_address,
      "locker_category" :this.locker_category
    }).subscribe(res=>{
      if(res.errorCode==0){
         alert(res.message);
         this.modify=false;
         this.getdata();         
      }    
      
    })
  }

  adddoor(){
    this.myHttp.post('door/create',
      {
        "door_name"  : this.door_name,
        "door_size" :this.door_size,
        "door_charge" :this.door_charge,
        "locker_id" :this.devdata.id,
        "door_number":this.door_number
      }).subscribe(res=>{
      if(res.errorCode==0){         
         alert(res.messages);
         this.myModal.hide(); 
         this.door_name = '';
         this.door_size = '';
         this.door_charge = '';
         this.getalldoor();             
      } 
    })   
  }

  editdoor(obj){
    this.btype=2;
    this.myModal.show();
    this.door_number = obj.door_number;
    this.door_name = obj.door_name;
    this.door_size = obj.door_size;
    this.door_charge = obj.door_charge;
    this.door_id = obj.id;
  }

  editdoorsave(){
    this.myHttp.post('door/update',
      {
        "door_name"  : this.door_name,
        "door_size" :this.door_size,
        "door_charge" :this.door_charge,
        "locker_id" :this.devdata.id,
        "door_number":this.door_number,
        "id" :this.door_id
      }).subscribe(res=>{
      if(res.errorCode==0){         
         alert(res.message);
         this.myModal.hide(); 
         this.door_name = '';
         this.door_size = -1;
         this.door_charge = -1;
         this.door_number = '';
         this.getalldoor();             
      } 
    })   
  }

  deldoor(name,id){
    this.btype=3;
    this.myModal.show();
    this.deldoorname = name;
    this.deldoorid = id;
  }

  deldoorsave(){
    this.myHttp.post('door/delete',
      {
        "id": this.deldoorid
      }).subscribe(res=>{
      if(res.errorCode==0){         
        //console.log(res) 
        alert(res.message);
        this.myModal.hide();
        this.getalldoor();    
      } 
    })   
  }


  getalldoor(){   
    if(this.devdata){      
        this.myHttp.post('door/findByLocker',
        {
          "locker_id":this.devdata.id

        }).subscribe(res=>{
        if(res.errorCode==0){         
          console.log(res) 
          this.doordata = res.message;         
        } 
      })   
    }
    
  }

  cancel(){
    this.door_name = '';
    this.door_number = '';
    this.door_size = -1;
    this.door_charge = -1;

  }


}

